package com.hspedu.homework.Homework01;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author DL5O
 * @version 1.0
 */
public class Server {
    public static void main(String[] args) throws Exception {
        String charset = "utf-8";//编码格式
        String name = "nova";
        String hobby = "编写java程序";

        ServerSocket serverSocket = new ServerSocket(9999);
        //进行监听
        System.out.println("等待客户端连接...");
        Socket socket = serverSocket.accept();
        String info = "";
        while (true) {
            String data = "";
            InputStream is = socket.getInputStream();
            info = StreamUtils.streamToString(is);
            if (info.equals("name")) {
                System.out.println("客户端获取名字...");
                data = "我是" + name;
            } else if (info.equals("hobby")) {
                System.out.println("客户端获取爱好...");
                data = "我的爱好" + hobby;
            } else if (info.equals("quit")) {
                System.out.println("客户端退出...");
                data = "已成功退出客户端";
            } else {
                data = "你说啥呢？";
            }
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            bw.write(data);
            bw.flush();
            if(info.equals(info.equals("quit"))){
                bw.close();
                break;
            }
        }
        socket.shutdownOutput();
        socket.close();
        System.out.println("服务端退出...");
    }
}
